//
//  MyComplaintsView.m
//  HackathonApp
//
//  Created by Bushra on 12/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyComplaintsView.h"
#import "ComplaintsDetailViewController.h"

@interface MyComplaintsView ()

@end

@implementation MyComplaintsView
@synthesize countries;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    docsDir = [dirPaths objectAtIndex:0];
    
    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
    [self findComplaints];
   // self.countries = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"]];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(IBAction) backButtonAction
{
	[self dismissModalViewControllerAnimated:YES];
    	//[self autorelease];
   
}

- (void) findComplaints
{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;
    
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat: @"SELECT traking_id,problem FROM problems"];
        
        const char *query_stmt = [querySQL UTF8String];
        
        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(statement) == SQLITE_ROW) {
                // Read the data from the result row
                
                NSString *aBrandName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
                NSString *aItemName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
               
                NSLog(@"SQL Compiled");
                
                self.countries = [NSDictionary dictionaryWithObjectsAndKeys:aBrandName,@"Issue",nil];
                
            }
            } 
        else {
                //   status.text = @"Match not found";
                //address.text = @"";
                // phone.text = @"";
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
   
}


/* UITABLEVIEW Deligate Methods */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.countries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CountryCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    NSString *country = [self.countries objectForKey:@"Issue"] ;
    
    cell.textLabel.text = country;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * issueID = [self.countries objectForKey:@"Issue"];
    
    
    
    ComplaintsDetailViewController* ViewController=[[ComplaintsDetailViewController alloc] initWithNibName:nil bundle:nil];
    ViewController.issueIDText=issueID;
	[self presentModalViewController:ViewController animated:YES];
    [ViewController release];
    
    
   /* UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"You selected %@!", country] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];*/
    
  //  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
